#define TB_IMPL
#define TB_LIB_OPTS
#include "termbox2.h"

#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv) {
    srand(time(NULL));

    (void)argc;
    (void)argv;

    tb_init();
    tb_set_input_mode(TB_INPUT_MOUSE);
    tb_set_output_mode(TB_OUTPUT_TRUECOLOR);

    // 1003 "Any-event tracking" mode
    // 1006 SGR extended coordinates (already enabled with TB_INPUT_MOUSE)
    tb_sendf("\x1b[?%d;%dh", 1003, 1006);
    tb_present();

    struct tb_event e;
    int rv;
    while (1) {
        rv = tb_poll_event(&e);
        if (!(rv == TB_OK || (rv == TB_ERR_POLL && tb_last_errno() == EINTR))) {
            break;
        } else if (e.type == TB_EVENT_KEY && e.ch == 'q') {
            break;
        } else if (e.type == TB_EVENT_MOUSE) {
            tb_print(e.x, e.y, 0, rand() % 0xffffff, " ");
            tb_set_cursor(e.x, e.y);
            tb_printf(0, 0, 0, 0, "key=%d mod=%d x=%d y=%d ", e.key, e.mod, e.x, e.y);
        }
        tb_present();
    }

    tb_sendf("\x1b[?%d;%dl", 1003, 1006);
    tb_shutdown();

    return 0;
}
